<?php
/********************************************************
 * Component: Common																			*
 *	Author: TuyenND																				*
 *	Date: 20-09-2007																			*
 * Note: All functions in this component can be changed	*
 * belong to each project																*
 ********************************************************/
class ImageUtilComponent extends Component {

	public function Convert_Image($From_Image_File_Path, $To_Image_File_Path, $Max_Width, $Max_Height)
	{
		$return_val = 1;
		$return_val = (($img = ImageCreateFromJPEG($From_Image_File_Path)) && $return_val == 1) ? "1" : "0";

		$FullImage_Width = imagesx($img);
		$FullImage_Height = imagesy($img);

		$Ratio = ($FullImage_Width > $Max_Width) ? (real)($Max_Width / $FullImage_Width) : 1;
		$New_Width = ((int)($FullImage_Width * $Ratio));
		$New_Height = ((int)($FullImage_Height * $Ratio));

		$Ratio = ($New_Height > $Max_Height) ? (real)($Max_Height / $New_Height) : 1;
		$New_Width = ((int)($New_Width * $Ratio));
		$New_Height = ((int)($New_Height * $Ratio));

		if ($New_Width == $FullImage_Width && $New_Height == $FullImage_Height)
		{ copy($From_Image_File_Path, $To_Image_File_Path); }

		$dst = ImageCreateTrueColor($New_Width, $New_Height);
		ImageCopyResampled($dst, $img, 0, 0, 0, 0, $New_Width, $New_Height, $FullImage_Width, $FullImage_Height);

		$return_val = ($full = ImageJPEG($dst, $To_Image_File_Path, 100) && $return_val == 1) ? "1" : "0";
		ImageDestroy($dst);

		return ($return_val) ? TRUE : FALSE;
	}

	//ham kiem tra dung luong file
	public function check_File_Size($filename)
	{
		if (filesize($filename) > 1500000) { return "toobig"; }
		else if (filesize($filename) > 1000000) { return "big"; }
		else { return "OK"; }
	}

	//ham kiem tra kich thuoc anh
	public function Image_Size_Check($imagename)
	{
		list($height, $width, $type, $attr) = getimagesize($imagename);

		if ($height < 300 && width < 300) { return true; }
		else { return false; }
	}

	public function splitString()
	{
	}

	public function concatString()
	{
	}

	public function createJPEG($new_w, $new_h, $srcImage, $destImage)
	{
		
		$destimg = imagecreatetruecolor($new_w, $new_h)or die('Problem In Creating image');
		$srcimg = ImageCreateFromJPEG($srcImage)or die('Problem In opening Source Image');
		if(!ImageCopyResampled($destimg, $srcimg, 0, 0, 0, 0, $new_w, $new_h, imagesx($srcimg),imagesy($srcimg)))
		return false;
		if(!imagejpeg($destimg, $destImage, 90))
		return false;
		imagedestroy($destimg);
		return true;
	}

	public function createGIF($new_w, $new_h, $srcImage, $destImage)
	{
		$destimg = imagecreatetruecolor($new_w, $new_h)or die('Problem In Creating image');
		$srcimg = imagecreatefromgif($srcImage)or die('Problem In opening Source Image');
		if(!ImageCopyResampled($destimg, $srcimg, 0, 0, 0, 0, $new_w, $new_h, imagesx($srcimg),imagesy($srcimg)))
		return false;
		if(!imagegif($destimg, $destImage, 90))
		return false;
		imagedestroy($destimg);
		return true;
	}

	public function createPNG($new_w, $new_h, $srcImage, $destImage)
	{
		$destimg = imagecreatetruecolor($new_w, $new_h)or die('Problem In Creating image');
		$srcimg = imagecreatefrompng($srcImage)or die('Problem In opening Source Image');
		if(!ImageCopyResampled($destimg, $srcimg, 0, 0, 0, 0, $new_w, $new_h, imagesx($srcimg),imagesy($srcimg)))
		return false;
		if(!imagepng($destimg, $destImage, 9))
		return false;
		imagedestroy($destimg);
		return true;
	}

	public function createBMP($new_w, $new_h, $srcImage, $destImage)
	{
		$destimg = imagecreatetruecolor($new_w, $new_h)or die('Problem In Creating image');
		$srcimg = imagecreatefromwbmp($srcImage)or die('Problem In opening Source Image');
		if(!ImageCopyResampled($destimg, $srcimg, 0, 0, 0, 0, $new_w, $new_h, imagesx($srcimg),imagesy($srcimg)))
		return false;
		if(!imagewbmp($destimg, $destImage, 90))
		return true;
		imagedestroy($destimg);
	}

	public function getFileType($filename)
	{
		$name = pathinfo($filename);
		return strtolower($name['extension']);
	}

	public function uploadFile($userfile_tmp, $destFile, $mode)
	{
		if (move_uploaded_file($userfile_tmp, $destFile))
		{
			chmod($destFile, octdec($mode));
			return true;
		}

		return false;
	}


	public function convertImageSize($mSrcImage, $mDestImage, $mNewWidth=0,$mNewHeight=0,$flag=0)
	{
		list($width,$height, $type, $attr) = getimagesize($mSrcImage);
		$type = $this->getFileType($mSrcImage);
		
		$ratio=$mNewWidth/$width;
		if($ratio>$mNewHeight/$height)$ratio=$mNewHeight/$height;
		$new_w=$width*$ratio;
		$new_h=$height*$ratio;
		
	
		ini_set('memory_limit', '128M');
		if (strtolower($type) == 'jpg' || strtolower($type) == 'jpeg')
		{ return @$this->createJPEG($new_w, $new_h, $mSrcImage, $mDestImage);}
		else if (strtolower($type) == 'gif')
		{ return @$this->createGIF($new_w, $new_h, $mSrcImage, $mDestImage); }
		else if (strtolower($type) == 'png')
		{ return @$this->createPNG($new_w, $new_h, $mSrcImage, $mDestImage); }
		else if (strtolower($type) == 'bmp')
		{ return @$this->createBMP($new_w, $new_h, $mSrcImage, $mDestImage); }
	}



	public function deleteFile($file) { return @unlink($file); }

	public function createBarChart($arrAssociation, $widthImage, $heightImage, $titleOfChart, $path)
	{
		$image = imagecreate($widthImage, $heightImage);

		$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
		$navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
		$black = imagecolorallocate($image, 0x00, 0x00, 0x00);
		$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);

		$maxValueOfArr = 80;
		$numValueOfArr = sizeof($arrAssociation);

		$verMargin = 20;
		$horMargin = 40;

		$distanceBetweenColumns = floor(($widthImage - $horMargin) / $numValueOfArr);

		$widthChartSize = $distanceBetweenColumns * $numValueOfArr;
		$heightChartSize = $heightImage - $verMargin * 2;

		$titleFont = 3;
		$titleWidth = imagefontwidth($titleFont) * strlen($titleOfChart);
		$titleHeight = imagefontheight($titleFont);

		$xTitle = ($horMargin + ($widthChartSize - $titleWidth) / 2);
		$yTitle = ($heightImage - $verMargin - $titleHeight - $heightChartSize);
		imagestring($image, $titleFont, $xTitle, $yTitle, $titleOfChart, $black);

		$labelFont = 2;
		$numGridLine = 4;
		$dyValue = $maxValueOfArr / $numGridLine;
		$dypValue = $heightChartSize / ($numGridLine + 1);

		for ($i = 0; $i <= ($numGridLine + 1); $i++)
		{
			$ydata = (int)($i * $dyValue);

			// height of grid line in pixels
			$yLabel = $verMargin + $heightChartSize - (int)($i * $dypValue);

			$labelWidth = imagefontwidth($labelFont) * strlen($ydata); // pixel-width of label
			$labelHeight = imagefontheight($labelFont);                // pixel-height of label

			$xLabel = (int)(($horMargin - $labelWidth) / 2);
			$xLabel = max(1, $xLabel);

			imagestring($image, $labelFont, $xLabel, $yLabel - (int)($labelHeight / 2), $ydata . ' %', $black);

			if (!($i == 0) && !($i > $numGridLine))
			{ imageline($image, $horMargin - 3, $yLabel, $horMargin + $widthChartSize, $yLabel, $gray); }
		}

		$padding = 3;
		$yscale = $heightChartSize / (($numGridLine + 1) * $dyValue);

		for ($i = 0; list($xval, $yval) = each($arrAssociation); $i++)
		{
			$ymax = $verMargin + $heightChartSize;
			$ymin = $ymax - (int)($yval * $yscale);
			$xmax = $horMargin + ($i + 1) * $distanceBetweenColumns - $padding;
			$xmin = $horMargin + $i * $distanceBetweenColumns + $padding;

			imagefilledrectangle($image, $xmin, $ymin, $xmax, $ymax, $navy);

			$txtsz = imagefontwidth($labelFont) * strlen($xval);

			$xpos = $xmin + (int)(($distanceBetweenColumns - $txtsz) / 2);
			$xpos = max($xmin, $xpos);
			$ypos = $ymax + 3;

			imagestring($image, $labelFont, $xpos, $ypos, $xval, $black);
		}

		$y = 0;
		$key = array_keys($arrAssociation);

		for ($i = 0; $i < count($arrAssociation); $i++)
		{
			$y = $y + 20;

			if ($key[$i] != "") { imagestring($image, $labelFont, $widthImage, $y, $key[$i], $black); }
		}

		imagerectangle($image, $horMargin, $verMargin, $horMargin + $widthChartSize, $verMargin + $heightChartSize,
		$black);

		//		header("Content-type: image/gif");
		imagegif($image, $path);
		imagedestroy($image);
	}

	public function Create_Verification_Image($width, $height, $path)
	{
		$img = imagecreate($width, $height)or die("Can't not create image!");

		$white = imagecolorallocate($img, 255, 255, 255);
		$black = imagecolorallocate($img, 0, 0, 0);
		$bg    = imagecolorallocate($img,  148,195, 230);

		$string = md5(rand(0, 9999));
		$newString = substr($string, 10, 5);
		imagefill($img, 0, 0, $bg);

		$fontSize = 4;
		$fontWidth = imagefontwidth($fontSize);
		$fontHeight = imagefontheight($fontSize);
		$x = ($width - $fontWidth * 5) / 2;
		$y = ($height - $fontHeight) / 2;
		imagestring($img, $fontSize, $x, $y, $newString, $white);
		//header("Content-type: image/png");
		imagepng($img, $path);
		imagedestroy($img);
		return $newString;
	}





}
?>